package com.huiquan.icd.job;

import com.huiquan.icd.dao.IcdDiseaseExternalDao;
import com.huiquan.icd.dao.IcdDiseaseHospitalDao;
import com.huiquan.icd.domain.IcdDiseaseExternal;
import com.huiquan.icd.domain.IcdDiseaseHospital;
import com.huiquan.management.constant.MigrateCode;
import com.huiquan.management.service.MigrateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class IcdCodeJob {

	private static final Logger LOG = LoggerFactory.getLogger(IcdCodeJob.class);

	@Autowired
	private IcdDiseaseHospitalDao icdDiseaseHospitalDao;
	@Autowired
	private IcdDiseaseExternalDao icdDiseaseExternalDao;
	@Autowired
	private MigrateService migrateService;

	/**
	 * code导出
	 */
	public void generateIcdCode() throws Exception {

		LOG.info("generateICDDiseaseHospital begin!");

		// 将数据输出到文件
		migrateService.migrateHiveByCodeAndString(MigrateCode.DISEASE_HOSPITAL, getDiseaseHospitalData());
		
		migrateService.migrateHiveByCodeAndString(MigrateCode.DISEASE_HOSPITAL_OTHER, getDiseaseOtherData());
		
		LOG.info("generateICDDiseaseHospital end!");
	}
	/**
	 * code导出
	 */
	public void generateOtherCode() throws Exception {

		LOG.info("generateOtherDiseaseHospital begin!");

		// 将数据输出到文件
		migrateService.migrateHiveByCodeAndString(MigrateCode.DISEASE_HOSPITAL_OTHER, getDiseaseOtherData());
		
		LOG.info("generateOtherDiseaseHospital end!");
	}
	public String getDiseaseOtherData() {
		// 获取可用的数据，生成疾病维表文件
		List<IcdDiseaseHospital> codeList = icdDiseaseHospitalDao.retrievesOtherTypeList();
		// 将数据拼成字符串
		StringBuffer buffer = new StringBuffer();
		for (IcdDiseaseHospital code : codeList) {
			buffer.append(code.getDiseaseCode());
			buffer.append((char) 1);
			buffer.append(code.getZdms());
			buffer.append((char) 1);
			buffer.append(code.getDiseaseName());
			buffer.append((char) 1);
			buffer.append(code.getHospitalId());
			buffer.append((char) 1);
			buffer.append(code.getDepartmentId());
			buffer.append((char) 1);
			buffer.append(code.getCategory());
			buffer.append((char) 1);
			buffer.append(code.getDisable());
			buffer.append((char) 1);
			buffer.append(code.getCategoryId());
			buffer.append((char) 1);
			buffer.append(code.getDiseaseId());
			buffer.append((char) 1);
			buffer.append(code.getCnt());
			buffer.append("\n");
		}

		return buffer.toString();
	}
	
	public String getDiseaseHospitalData() {
		// 获取可用的数据，生成疾病维表文件
		List<IcdDiseaseHospital> codeList = icdDiseaseHospitalDao.retrievesCodeTypeList();
		// 将数据拼成字符串
		StringBuffer buffer = new StringBuffer();
		for (IcdDiseaseHospital code : codeList) {
			buffer.append(code.getDiseaseCode());
			buffer.append((char) 1);
			buffer.append(code.getDiseaseName());
			buffer.append((char) 1);
			buffer.append(code.getHospitalId());
			buffer.append((char) 1);
			buffer.append(code.getDepartmentId());
			buffer.append((char) 1);
			buffer.append(code.getCategory());
			buffer.append((char) 1);
			buffer.append(code.getDisable());
			buffer.append((char) 1);
			buffer.append(code.getCategoryId());
			buffer.append((char) 1);
			buffer.append(code.getDiseaseId());
			buffer.append((char) 1);
			buffer.append(code.getCnt());
			buffer.append("\n");
		}

		return buffer.toString();
	}
	
	public void generateTZDisease() throws Exception {

		LOG.info("generateTZdisease begin!");
		List<IcdDiseaseExternal> list = icdDiseaseExternalDao.retrievesExportTZList();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("disable", "0");
		List<String> diseases = icdDiseaseExternalDao.getAllDiseaseNameWithOutSynonymy(param);
		
		// 将数据拼成字符串
		StringBuffer buffer = new StringBuffer();
		for (IcdDiseaseExternal code : list) {
			buffer.append(code.getDiseaseName());
			buffer.append((char) 1);
			buffer.append(code.getCnt());
			buffer.append((char) 1);
			buffer.append(code.getShowDisease());
			buffer.append((char) 1);
			buffer.append(code.getDisable());
			buffer.append((char) 1);
			buffer.append(code.getCategoryName());
			buffer.append((char) 1);
			if(diseases.contains(code.getShowDisease())){
				buffer.append("1");
			}else{
				buffer.append("0");
			}
			buffer.append("\n");
		}
		
		// 将数据输出到文件
		migrateService.migrateFileByCodeAndString(MigrateCode.DISEASE_EXTERNAL_TZ, buffer.toString());
		
		LOG.info("generateTZdisease end!");
	}
	
	
}
